package com.huateng.service;

import com.huateng.util.HbaseUtils;
import com.huateng.util.RedisUtils;
import com.huateng.vo.CustomerReqVo;
import com.huateng.vo.ResponseVo;
import com.huateng.vo.TradeFlowVo;
import org.apache.hadoop.hbase.filter.Filter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by joy on 2016/11/7.
 */
@Service
public class JudicialService {
    @Resource
    private HbaseUtils hbaseUtils;
    @Resource
    private RedisUtils redisUtils;

    @Value("${judicial.hbase.customer}")
    private String customerInfoHbaseName;
    @Value("${judicial.hbase.tradeflow}")
    private String tradeflowHbaseName;

    public ResponseVo findCustomerInfo(String id, String startrowkey, int pagesize) {
        CustomerReqVo customerReqVo = new CustomerReqVo(id,startrowkey,pagesize);
        ResponseVo vo = redisUtils.getPageCache(customerReqVo.toString());
        if(vo == null){
            Filter filter = hbaseUtils.generatePreFilter(id);
            List<Map<String,String>> value = hbaseUtils.querySimple(customerInfoHbaseName,startrowkey,pagesize,filter);
            vo = new ResponseVo();
            if(value.size() !=0 ){
                vo.setPagesize(pagesize);
                vo.setSize(value.size());
                vo.setValues(value);
                vo.setEndrowkey(value.get(value.size()-1).get(HbaseUtils.ROWKEY));
                vo.setStartrowkey(value.get(0).get(HbaseUtils.ROWKEY));
            }
            redisUtils.putPageCache(customerReqVo.toString(),vo);
        }
        return vo;
    }

    public ResponseVo findTradeFlow(String card, String begindate, String enddate, String startrowkey, int pagesize) {
        TradeFlowVo tradeFlowVo = new TradeFlowVo(card,begindate,enddate,startrowkey,pagesize);
        ResponseVo vo = redisUtils.getPageCache(tradeFlowVo.toString());
        if(vo == null){
            Filter filter = hbaseUtils.generateFilter(card,begindate,enddate);
            List<Map<String,String>> value = hbaseUtils.querySimple(tradeflowHbaseName,startrowkey,pagesize,filter);
            vo = new ResponseVo();
            if(value.size() !=0 ){
                vo.setPagesize(pagesize);
                vo.setSize(value.size());
                vo.setValues(value);
                vo.setEndrowkey(value.get(value.size()-1).get(HbaseUtils.ROWKEY));
                vo.setStartrowkey(value.get(0).get(HbaseUtils.ROWKEY));
            }
            redisUtils.putPageCache(tradeFlowVo.toString(),vo);
        }
        return vo;
    }
}
